/**
 * Created by xuxujunjunforever on 2015/12/19.
 */
'use strict'
let models = require('../models');
let config = require('../../config');

let PrivilegeDao = models.PrivilegeDao;

let Privilege = models.Privilege;

let actionCreatePrivilege = {
    privileges:['manage_privilege'],
    fields:{
        privilege:{
            name: true
        }
    },
    handler(req,res) {
        if ( !req.session.login ) {
            throw config.errors.UserNotLogin;
            return;
        }

        let privilege = new Privilege(req.body.privilege);

        PrivilegeDao.create(privilege);

        let privilegeInfomation = privilege.toObject({recursive:true});

        res.json({
            successful:true,
            data:{
                privilege:privilegeInfomation
            }
        });
    }
};

let actionRefreshPrivilege = {
    privileges:['manage_privilege'],
    fields:{
        privilege:{
            id: true
        }
    },
    handler(req,res){
        if ( !req.session.login ) {
            throw config.errors.UserNotLogin;
            return;
        }

        let privilege = req.body.privilege;

        let refreshprivilege = PrivilegeDao.findOne({
            id:privilege.id
        });
        if(refreshprivilege == null){
            throw config.errors.PrivilegeCanNotFound;
            return;
        }

        let privilegeInfomation = refreshprivilege.toObject({recursive:true});

        res.json({
            successful:true,
            data:{
                privilege:privilegeInfomation
            }
        });
    }
};

let actionUpdatePrivilege = {
    handler(req,res) {
        if ( !req.session.login ) {
            throw config.errors.UserNotLogin;
            return;
        }

        let newprivilege = req.body.privilege;

        let oldprivilege = PrivilegeDao.findOne({
            id:newprivilege.id
        });
        if(oldprivilege == null){
            throw config.errors.PrivilegeCanNotFound;
            return;
        }

        PrivilegeDao.update(newprivilege);

        let privilegeInfomation = newprivilege.toObject({recursive:true});

        res.json({
            successful:true,
            data:{
                privilege:privilegeInfomation
            }
        });
    }
};

let actionRemovePrivilege = {
    privileges:['manage_privilege'],
    fields:{
        privilege:{
            id: true
        }
    },
    handler(req,res) {
        if ( !req.session.login ) {
            throw config.errors.UserNotLogin;
            return;
        }

        let privilege = PrivilegeDao.findOne({
            id:req.body.privilege.id
        });
        if(privilege == null){
            throw config.errors.PrivilegeCanNotFound;
            return;
        }

        PrivilegeDao.remove(privilege);

        res.json({
            successful:true
        });
    }
};

let actionPrivilegeAll = {
    login:true,
    handler(req,res) {
        if ( !req.session.login ) {
            throw config.errors.UserNotLogin;
            return;
        }

        let privileges = PrivilegeDao.find({});
        let plainprivileges = Privilege.toObjectArray(privileges,{recursive:true});

        res.json({
           successful:true,
            data:{
                privileges:plainprivileges
            }
        });
    }
};

module.exports = {
    actionCreatePrivilege,
    actionRefreshPrivilege,
    //actionUpdatePrivilege,
    actionRemovePrivilege,
    actionPrivilegeAll
};
